<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://www.springframework.org/tags/form"  prefix="sf"%>


<c:set var="ctx" value="${pageContext.request.contextPath }" />
<link rel="stylesheet"
	href="${ctx }/resources/js/select2/select2.min.css" />
 
<script type="text/javascript"
	src="${ctx }/resources/js/select2/select2.min.js"></script>
<script type="text/javascript"
	src="${ctx }/resources/js/select2/zh-CN.js"></script>
<script type="text/javascript">
$(function() {
	webside.form.localad.initResource();
	
});
$("#typeName").change(function(){
	$("#type").val($("#typeName").find("option:selected").attr("typecode"));

});
	$(function() {
		webside.form.localad.validateLocaladForm();
	})
	
	$("#resourceType").change(function(){
		var rt=$(this).val();
		if(rt=="视频广告"){
			$("#uploadhide").hide();
			$("#resourceurl").show();
		}else{
				$("#resourceurl").hide();
				$("#uploadhide").show();
		};
	});
	$("#kind").change(function(		
	){
		$("#resourceName").val("");
		$("#seriesId").val("");
		var $this=$(this);
		var opid=$(this).find("option");
		$("#resourceName").attr("placeholder","剧集类型与剧集资源只能二选一,您已选定剧集类型");
		for(var i=0;i<opid.length;i++){
			if(opid.eq(i).val()==$this.val())$("#kindName").val(opid.eq(i).text());
		}
	
	});
	$("#resourceName").on("focus",function(		
	){
		$("#kind").val("");
		$("#kindName").val("");
		var opid=$("#kind").find("option");
		opid.eq(0).text("剧集资源与剧集类型只能二选一,您已填选剧集资源");
	
	});
	
	function doSaveAd() {
		var fileName = $("#cover").val();
		var type = fileName.split(".");
		var ftype = type[type.length - 1]; //获取文件后缀
		$.ajaxFileUpload({
			url :'${ctx }/files/upload/' + ftype,
			secureuri : false, //是否需要安全协议，一般设置为false
			fileElementId : [ 'cover' ], //文件上传域的ID
			dataType : 'json', //返回值类型 一般设置为json
			success : function(data, status) //服务器成功响应处理函数
			{ 
				if (data.result == 0) {
					var fileId = data.data[0].id;
					var filePath = '/files/view/jpg/' + fileId;
					$("#resourceUrl").val(filePath);
					//显示图片
					$("#imgCover").attr("src",'${ctx }/files/view/jpg/' + fileId);
					//显示名字
					$("#labcover").html(data.data[0].name);
				}
			},
			error : function(data, status, e) //服务器响应失败处理函数
			{
				alert(e);
			}
		});
		
	}
</script>

<%-- <script src="${basePath}/script/ajaxfileupload.js" type="text/javascript"></script>
  --%>

<script src="${ctx }/resources/js/upload/ajaxfileupload.js"
	type="text/javascript">
</script>
<script type="text/javascript">

 
    
//光标在第一行第一个
function s(e,a)
{
	 if ( e && e.preventDefault )
            e.preventDefault();
	else 
	window.event.returnValue=false;
		a.focus();
		
}

function uploadVideoResource() {
    var fileName = $("#cover").val();
    var type = fileName.split(".");
    var ftype = type[type.length - 1]; //获取文件后缀
    $.ajaxFileUpload({
        url: '${ctx }/files/upload/' + ftype,
        secureuri: false, //是否需要安全协议，一般设置为false
        fileElementId: ['cover'], //文件上传域的ID
        dataType: 'json', //返回值类型 一般设置为json
        success: function (data, status) //服务器成功响应处理函数
        {
        	 
            if (data.result == 0) {
            	var fileId = data.data[0].id;
            	var filePath = '/files/view/jpg/' + fileId;
            	$("#hiddenResourceUrl").val(filePath);
            	//显示图片
				$("#imgCover").attr("src",'${ctx}/files/view/jpg/' + fileId);
				//显示名字
				$("#labcover").html(data.data[0].name);
            }
        },
        error: function (data, status, e) //服务器响应失败处理函数
        {
            alert(e);
        }
    });
}
</script>

<div class="page-header">
	<h1>
		<c:if test="${empty advertisementEntity}">
		新增本地广告
		</c:if>
		<c:if test="${!empty advertisementEntity}">
		编辑本地广告
		</c:if>
	</h1>
</div>
<div class="row" style="margin-top: 5px;">
	<div class="col-xs-12">
		<!--  <form action="localad/addinsert.html" id="addUI" name="addUI" class="form-horizontal" role="form" method="post">-->
		<form id="localadForm" name="localadForm" class="form-horizontal"
			role="form" method="post">
			<c:if test="${!empty advertisementEntity}">
				<input type="hidden" id="pageNum" name="pageNum"
					value="${page.pageNum }">
				<input type="hidden" id="pageSize" name="pageSize"
					value="${page.pageSize }">
				<input type="hidden" id="orderByColumn" name="orderByColumn"
					value="${page.orderByColumn }">
				<input type="hidden" id="orderByType" name="orderByType"
					value="${page.orderByType }">
				<input type="hidden" id='id' name="id" value="${advertisementEntity.id }">
			</c:if>
				
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="title">广告标题</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="title" id="title" type="text"
							value="${advertisementEntity.title }" placeholder="广告标题" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right"
					for="owner">广告商</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="owner" id="owner"
							type="text" value="${advertisementEntity.owner }" placeholder="广告商"  maxlength='10'/>
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="resourceType">资源类型</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<select id="resourceType" class="form-control" name='resourceType'>
						    <option value="" <c:if test="${advertisementEntity.resourceType eq null}">selected="selected"</c:if>>请选择</option>
							<c:forEach items="${rstype_list}" var="t">
							<option value="${t.name}"<c:if test="${advertisementEntity.resourceType eq t.name}">selected="selected"</c:if>>${t.name}</option>
							</c:forEach>
						</select>
						
					</div>
				</div>
			</div>
			<div  class="form-group" id="uploadhide">
			<label class="control-label col-sm-1 no-padding-right" for="cover">广告</label>
			<div class="col-sm-10">
				<div class="clearfix">
					<input class="form-control" name="files" id="cover" type="file"
		                  value="${bannerEntity.imgUrl }" />
		                 <img id="imgCover" alt="" 
		                 <c:if test="${'' eq  advertisementEntity.resourceUrl || null eq advertisementEntity.resourceUrl}">src="${ctx}/resources/images/xx.png"</c:if> 
		                 <c:if test="${advertisementEntity.resourceUrl != null}">src="${ctx}${advertisementEntity.resourceUrl}"</c:if>
		                  width="175px" height="100px">
						<label id="labcover" ></label>                
					<input class="btn btn-primary btn-sm" type="button" value="上传" onclick="doSaveAd();"/>
					 <input id="hiddenResourceUrl" name="imgUrl" type="hidden" />
				 </div>
			</div>
			</div>
			<div  class="form-group" id="resourceurl">
				<label class="control-label col-sm-1 no-padding-right" for="resourceUrl">资源地址</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="resourceUrl" id="resourceUrl" type="text"
							value="${advertisementEntity.resourceUrl }" placeholder="资源地址" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="linkUrl">链接</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="linkUrl"  	id="linkUrl" type="text"
							value="${advertisementEntity.linkUrl }" placeholder="完整链接" />
							<!-- onblur="iscontain(this)" -->
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="type">类型</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<select id="typeName" class="form-control" name='typeName' id="typeName">
						    <option typecode="" value="" <c:if test="${advertisementEntity.typeName eq null}">selected="selected"</c:if>>请选择</option>
							<c:forEach items="${type_list}" var="t">
							<option typecode='${t.code}' value="${t.name}"<c:if test="${advertisementEntity.typeName eq t.name}">selected="selected"</c:if>>${t.name}</option>
							</c:forEach>
						</select>
						<input id="type" name="type" type="text" style="display:none" value="${advertisementEntity.type}"/>
					</div>
				</div>
			</div>
			
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="sDate">开始时间</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="sDate" onfocus="HS_setDate(this)" id="sDate" type="text"
							value="${sdate }" placeholder="格式如：2010-10-01" />
					</div>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="eDate">截止时间</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="eDate" onfocus="HS_setDate(this)" id="eDate" type="text"
							value="${edate }" placeholder="格式如：2010-10-01" />
					</div>
				</div>
			</div>

			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="level">广告等级</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<select id="level" class="form-control" name='level'>
						    <option value=""  <c:if test="${advertisementEntity.level eq null}" >selected="selected"</c:if>>请选择</option>
							<c:forEach items="${level_list}" var="t">
							<option value="${t.name}"<c:if test="${advertisementEntity.level eq t.code}">selected="selected"</c:if>>${t.name}</option>
							</c:forEach>
						</select>
						
					</div>
				</div>
			</div>

		 
			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="kinds">剧集类型</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<select id="kind" class="form-control" name='kind'>
						    <option value="" <c:if test="${advertisementEntity.kind eq null}">selected="selected"</c:if>>请选择(剧集类型与剧集资源只能二选一)</option>
							<c:forEach items="${stype_list}" var="t">
							<option value="${t.code}"<c:if test="${advertisementEntity.kind eq t.code}">selected="selected"</c:if>>${t.name}</option>
							</c:forEach>
						</select> 
						<input type="text" style="display:none;"" value="${advertisementEntity.kindName }" id="kindName" name="kindName" value="">
					</div>
				</div>
			</div>


			<div class="form-group">
				<label class="control-label col-sm-1 no-padding-right" for="seriesName">剧集资源</label>
				<div class="col-sm-10">
					<div class="clearfix">
						<input class="form-control" name="seriesName" id="resourceName" type="text"
							value="${advertisementEntity.seriesName }" placeholder="剧集资源(剧集资源与剧集类型只能二选一)" />
							<!-- <i id="resourceShow" data-rel="tooltip" data-placement="left"  data-original-title="点击清除" </i> -->
						<input id="seriesId" name="seriesId" value="${advertisementEntity.seriesId }" type="text" style="display:none;"/>
					</div>
				</div>
			</div>
			<input name="status" val="" style="display:none;"/>
		</form>		
		<div class="hr hr-dotted"></div>
	</div>
</div>
<div class="center">
	<button id="btnAdd" type="button"
		onclick="javascript:$('#localadForm').submit();"
		class="btn btn-success btn-sm">
		<i class="fa fa-user-plus"></i>&nbsp;
		<c:if test="${empty advertisementEntity}">
		添加
		</c:if>
		<c:if test="${!empty advertisementEntity}">
		保存
		</c:if>
	</button>
	<button id="btn" type="button"
		onclick="webside.common.loadPage('/localad/listUI.html<c:if test="${!empty advertisementEntity}">?page=${page.pageNum }&rows=${page.pageSize }&sidx=${page.orderByColumn }&sord=${page.orderByType }</c:if>')"
		class="btn btn-info btn-sm">
		<i class="fa fa-undo"></i>&nbsp;返回
	</button>
</div>
<script type="text/javascript">
function HS_DateAdd(interval,number,date){
	number = parseInt(number);
	if (typeof(date)=="string"){var date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])}
	if (typeof(date)=="object"){var date = date}
	switch(interval){
	case "y":return new Date(date.getFullYear()+number,date.getMonth(),date.getDate()); break;
	case "m":return new Date(date.getFullYear(),date.getMonth()+number,checkDate(date.getFullYear(),date.getMonth()+number,date.getDate())); break;
	case "d":return new Date(date.getFullYear(),date.getMonth(),date.getDate()+number); break;
	case "w":return new Date(date.getFullYear(),date.getMonth(),7*number+date.getDate()); break;
	}
}
function checkDate(year,month,date){
	var enddate = ["31","28","31","30","31","30","31","31","30","31","30","31"];
	var returnDate = "";
	if (year%4==0){enddate[1]="29"}
	if (date>enddate[month]){returnDate = enddate[month]}else{returnDate = date}
	return returnDate;
}

function WeekDay(date){
	var theDate;
	if (typeof(date)=="string"){theDate = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2]);}
	if (typeof(date)=="object"){theDate = date}
	return theDate.getDay();
}
function HS_calender(){
	var lis = "";
	var style = "";
	/*可以把下面的css剪切出去独立一个css文件knowsky.com*/
	style +="<style type='text/css'>";
	style +=".calender { width:170px; height:auto; font-size:12px; margin-right:14px; background:url(${ctx}/resources/images/calenderbg.gif) no-repeat right center #fff; border:1px solid #397EAE; padding:1px}";
	style +=".calender ul {list-style-type:none; margin:0; padding:0;}";
	style +=".calender .day { background-color:#EDF5FF; height:20px;}";
	style +=".calender .day li,.calender .date li{ float:left; width:14%; height:20px; line-height:20px; text-align:center}";
	style +=".calender li a { text-decoration:none; font-family:Tahoma; font-size:11px; color:#333}";
	style +=".calender li a:hover { color:#f30; text-decoration:underline}";
	style +=".calender li a.hasArticle {font-weight:bold; color:#f60 !important}";
	style +=".lastMonthDate, .nextMonthDate {color:#bbb;font-size:11px}";
	style +=".selectThisYear a, .selectThisMonth a{text-decoration:none; margin:0 2px; color:#000; font-weight:bold}";
	style +=".calender .LastMonth, .calender .NextMonth{ text-decoration:none; color:#000; font-size:18px; font-weight:bold; line-height:16px;}";
	style +=".calender .LastMonth { float:left;}";
	style +=".calender .NextMonth { float:right;}";
	style +=".calenderBody {clear:both}";
	style +=".calenderTitle {text-align:center;height:20px; line-height:20px; clear:both}";
	style +=".today { background-color:#ffffaa;border:1px solid #f60; padding:2px}";
	style +=".today a { color:#f30; }";
	style +=".calenderBottom {clear:both; border-top:1px solid #ddd; padding: 3px 0; text-align:left}";
	style +=".calenderBottom a {text-decoration:none; margin:2px !important; font-weight:bold; color:#000}";
	style +=".calenderBottom a.closeCalender{float:right}";
	style +=".closeCalenderBox {float:right; border:1px solid #000; background:#fff; font-size:9px; width:11px; height:11px; line-height:11px; text-align:center;overflow:hidden; font-weight:normal !important}";
	style +="</style>";

	var now;
	if (typeof(arguments[0])=="string"){
		selectDate = arguments[0].split("-");
		var year = selectDate[0];
		var month = parseInt(selectDate[1])-1+"";
		var date = selectDate[2];
		now = new Date(year,month,date);
	}else if (typeof(arguments[0])=="object"){
		now = arguments[0];
	}
	var lastMonthEndDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+now.getMonth()+"-01").getDate();
	var lastMonthDate = WeekDay(now.getFullYear()+"-"+now.getMonth()+"-01");
	var thisMonthLastDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-01");
	var thisMonthEndDate = thisMonthLastDate.getDate();
	var thisMonthEndDay = thisMonthLastDate.getDay();
	var todayObj = new Date();
	today = todayObj.getFullYear()+"-"+todayObj.getMonth()+"-"+todayObj.getDate();
	
	for (i=0; i<lastMonthDate; i++){  // Last Month's Date
		lis = "<li class='lastMonthDate'>"+lastMonthEndDate+"</li>" + lis;
		lastMonthEndDate--;
	}
	for (i=1; i<=thisMonthEndDate; i++){ // Current Month's Date

		if(today == now.getFullYear()+"-"+now.getMonth()+"-"+i){
			var todayString = now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-"+i;
			lis += "<li><a href=javascript:void(0) class='today' onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
		}else{
			lis += "<li><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>";
		}
		
	}
	var j=1;
	for (i=thisMonthEndDay; i<6; i++){  // Next Month's Date
		lis += "<li class='nextMonthDate'>"+j+"</li>";
		j++;
	}
	lis += style;

	var CalenderTitle = "<a href='javascript:void(0)' class='NextMonth' onclick=HS_calender(HS_DateAdd('m',1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Next Month'>&raquo;</a>";
	CalenderTitle += "<a href='javascript:void(0)' class='LastMonth' onclick=HS_calender(HS_DateAdd('m',-1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Previous Month'>&laquo;</a>";
	CalenderTitle += "<span class='selectThisYear'><a href='javascript:void(0)' onclick='CalenderselectYear(this)' title='Click here to select other year' >"+now.getFullYear()+"</a></span>年<span class='selectThisMonth'><a href='javascript:void(0)' onclick='CalenderselectMonth(this)' title='Click here to select other month'>"+(parseInt(now.getMonth())+1).toString()+"</a></span>月"; 

	if (arguments.length>1){
		arguments[1].parentNode.parentNode.getElementsByTagName("ul")[1].innerHTML = lis;
		arguments[1].parentNode.innerHTML = CalenderTitle;

	}else{
		var CalenderBox = style+"<div class='calender'><div class='calenderTitle'>"+CalenderTitle+"</div><div class='calenderBody'><ul class='day'><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class='date' id='thisMonthDate'>"+lis+"</ul></div><div class='calenderBottom'><a href='javascript:void(0)' class='closeCalender' onclick='closeCalender(this)'>&times;</a><span><span><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+todayString+"'>Today</a></span></span></div></div>";
		return CalenderBox;
	}
}
function _selectThisDay(d){
	var boxObj = d.parentNode.parentNode.parentNode.parentNode.parentNode;
		boxObj.targetObj.value = d.title;
		boxObj.parentNode.removeChild(boxObj);
		if($("#eDate").val().trim()!=="" && $("#sDate").val().trim()!==""){
			var thetime = document.getElementById("sDate").value;
			var   stime=new   Date(Date.parse(thetime .replace(/-/g,"/")));
			var endtime = document.getElementById("eDate").value;
			var   etime=new   Date(Date.parse(endtime .replace(/-/g,"/")));
			if(etime<=stime){
				$("#eDate").val("");
				$("#eDate").attr("placeholder","请选择开始时间之后日期");
			}
		};
}
function closeCalender(d){
	var boxObj = d.parentNode.parentNode.parentNode;
		boxObj.parentNode.removeChild(boxObj);
}

function CalenderselectYear(obj){
		var opt = "";
		var thisYear = obj.innerHTML;
		for (i=1970; i<=2020; i++){
			if (i==thisYear){
				opt += "<option value="+i+" selected>"+i+"</option>";
			}else{
				opt += "<option value="+i+">"+i+"</option>";
			}
		}
		opt = "<select onblur='selectThisYear(this)' onchange='selectThisYear(this)' style='font-size:11px'>"+opt+"</select>";
		obj.parentNode.innerHTML = opt;
}

function selectThisYear(obj){
	HS_calender(obj.value+"-"+obj.parentNode.parentNode.getElementsByTagName("span")[1].getElementsByTagName("a")[0].innerHTML+"-1",obj.parentNode);
}

function CalenderselectMonth(obj){
		var opt = "";
		var thisMonth = obj.innerHTML;
		for (i=1; i<=12; i++){
			if (i==thisMonth){
				opt += "<option value="+i+" selected>"+i+"</option>";
			}else{
				opt += "<option value="+i+">"+i+"</option>";
			}
		}
		opt = "<select onblur='selectThisMonth(this)' onchange='selectThisMonth(this)' style='font-size:11px'>"+opt+"</select>";
		obj.parentNode.innerHTML = opt;
}
function selectThisMonth(obj){
	HS_calender(obj.parentNode.parentNode.getElementsByTagName("span")[0].getElementsByTagName("a")[0].innerHTML+"-"+obj.value+"-1",obj.parentNode);
}
function HS_setDate(inputObj){
	var $this=$(inputObj);
	$(".calender").remove();
	var calenderObj = document.createElement("span");
	calenderObj.innerHTML = HS_calender(new Date());
	calenderObj.style.position = "relative";
	calenderObj.targetObj = inputObj;
	inputObj.parentNode.insertBefore(calenderObj,inputObj.nextSibling);
	$(".calender").bind("mouseleave",function(){
		$this.blur();
		$(this).remove();
	});
}
/* function iscontain(obj){
	var strings=$(obj).val();
	if(strings.indexOf("http://")!=0){
		$(obj).val("http://"+strings);
	} 
}*/

</script>